package com.wontube.smr.dao.bo;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.client.modle.vo.PagenateSupport;
import com.wontube.core.dao.mybatis.QueryExample;
import com.wontube.smr.bo.constants.SiteConstants;
import com.wontube.smr.client.model.po.Movie;
import com.wontube.smr.client.model.po.VVideo;
import com.wontube.smr.client.model.vo.VVideoItems;
import com.wontube.smr.dao.mapper.VVideoMapper;
import com.wontube.smr.pub.DefaultDBHelper;

/**
 * @author yuechao
 *
 */
public final class VideoSelectDao {
	private VideoSelectDao() {
	}
	
	public static VVideoItems searchVVideosForWeb(String keyword, PagenateInfo pageInfo) {
		VVideoItems items = new VVideoItems(pageInfo);
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			VVideoMapper mapper = session.getMapper(VVideoMapper.class);		
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("(v_video.website != 'Crunchyroll' or v_video.website !='YouTube')")
				.addCriterion(getSearchLikeString(keyword));
			
			ex.setOrderByClause("v_video.sort_index asc, v_video.type asc");
			RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
			
			List<VVideo> list = mapper.selectByExample(ex, rowBounds);
			items.setItems(list);			
		} finally {
			session.close();
		}
		return items;
	}
	
	public static VVideoItems searchVVideosForMobile(String keyword, PagenateInfo pageInfo) {
		VVideoItems items = new VVideoItems(pageInfo);
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			VVideoMapper mapper = session.getMapper(VVideoMapper.class);		
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("v_video.website != ", SiteConstants.WEBSITE_CRUNCHYROLL)
				.addCriterion("v_video.type !=", Movie.TYPE_TRAILER)
				.addCriterion(getSearchLikeString(keyword));
			
			ex.setOrderByClause("v_video.sort_index asc, v_video.type asc");
			RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
			
			List<VVideo> list = mapper.selectByExample(ex, rowBounds);
			items.setItems(list);			
		} finally {
			session.close();
		}
		return items;
	}
	
	public static VVideoItems searchVVideosForI18n(String keyword, PagenateInfo pageInfo) {
		VVideoItems items = new VVideoItems(pageInfo);
		SqlSession session = DefaultDBHelper.getSqlSession(false);
		try {
			VVideoMapper mapper = session.getMapper(VVideoMapper.class);		
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("(v_video.website='YouTube' or v_video.website='Imdb' or v_video.website='Crunchyroll')")
				.addCriterion(getSearchLikeString(keyword));
			
			ex.setOrderByClause("v_video.sort_index asc, v_video.type asc");
			RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
			
			List<VVideo> list = mapper.selectByExample(ex, rowBounds);			
			items.setItems(list);						
			session.commit();
		} finally {
			session.close();
		}
		return items;
	}
	
	private static String getSearchLikeString(String keyword){
		StringBuilder sb = new StringBuilder();
		keyword = keyword.replaceAll("[']", "''");
		sb.append("(v_video.title ilike '%" + keyword + "%'");
		sb.append(" or v_video.parent_title ilike '%" + keyword + "%'");
		sb.append(" or v_video.actors ilike '%" + keyword + "%'");
		sb.append(" or v_video.director ilike '%" + keyword + "%')");
		return sb.toString();
	}
	
	public static VVideoItems search(String website, String keyword, PagenateInfo pageInfo) {
		VVideoItems items = new VVideoItems();
		PagenateSupport support = items.getPagenate();
		support.setPagenateInfo(pageInfo);
		
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			VVideoMapper mapper = session.getMapper(VVideoMapper.class);
			
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("v_video.website = ", website)
				.addCriterion(getSearchLikeString(keyword));
			
			ex.setOrderByClause("v_video.sort_index asc, v_video.type asc");
			RowBounds rowBounds = new RowBounds(pageInfo.getOffset(), pageInfo.getLimit());
			
			List<VVideo> list = mapper.selectByExample(ex, rowBounds);
			items.setItems(list);
			int totalCount = mapper.countByExample(ex);
			support.setTotalCount(totalCount);
		} finally {
			session.close();
		}
		
		return items;
	}
	
	public static int priviewCount(String keyword, String website) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {			
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("v_video.website = ", website)
					.addCriterion(getSearchLikeString(keyword));
			
			VVideoMapper mapper = session.getMapper(VVideoMapper.class);
			return mapper.countByExample(ex);
		} finally {
			session.close();
		}
	}
	
	public static String getThumbnail(String url) {
		SqlSession session = DefaultDBHelper.getSqlSession(true);
		try {
			VVideoMapper mapper = session.getMapper(VVideoMapper.class);
			QueryExample ex = new QueryExample();
			ex.or().addCriterion("v_video.item_url=", url);
			RowBounds rowBounds = new RowBounds(0, 1);
			List<VVideo> list = mapper.selectByExample(ex, rowBounds);
			if (list.size() > 0) {
				return list.get(0).getThumbnail();
			}
		} finally {
			session.close();
		}
		
		return "";
	}
}
